package com.hospital.register.common.utils;

import org.joda.time.DateTime;

import java.util.Date;

public final class DateUtil {

    private DateUtil() {
    }

    /**
     * 一天的毫秒数
     */
    public static final long MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;

    /**
     * 根据 DateTime 获得星期（中文）
     *
     * @param dateTime 日期
     * @return 星期（中文）
     */
    public static String getChineseDayOfWeek(DateTime dateTime) {
        if (dateTime.getDayOfWeek() >= 1 && dateTime.getDayOfWeek() <= 6) {
            return "周" + dateTime.getDayOfWeek();
        } else {
            return "周日";
        }
    }

    /**
     * 根据 Calendar 对象返回的数字星期获得中文星期
     *
     * @param dayOfWeek Calendar 对象返回的星期
     * @return 中文星期
     */
    public static String getChineseDayOfWeek(int dayOfWeek) {
        if (dayOfWeek >= 2 && dayOfWeek <= 7) {
            return "周" + (dayOfWeek - 1);
        } else if (dayOfWeek == 1) {
            return "周日";
        } else {
            return "未知";
        }
    }

    /**
     * 得到当天的凌晨零点
     *
     * @param dateTime 当天时间
     * @return 当天的凌晨零点
     */
    public static Date getStartOnOneDay(Date date) {
        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDate();
        return new Date(year, month, day);
    }

    public static String formatDate(Date date) {
        int year = date.getYear() + 1900;
        int month = date.getMonth() + 1;
        int dayOfMonth = date.getDate();

        StringBuilder dateBuilder = new StringBuilder();
        dateBuilder.append(year).append("-");

        if (month < 10) {
            dateBuilder.append("0");
        }
        dateBuilder.append(month).append("-");

        if (dayOfMonth < 10) {
            dateBuilder.append("0");
        }
        dateBuilder.append(dayOfMonth);

        return dateBuilder.toString();
    }

}
